#!/bin/bash
set -e

# 定义ssh执行的命令
#cmd="cat /opt/job/cleanup-scripts/clean-common-logs.sh"

# 将命令行参数赋值给变量cmd
cmd=$1

# 判断变量是否为空
if [[ -z $cmd ]]; then
   cmd="'bash -s' <./add-job.sh"
fi

echo "是否在"
cat servers.txt
echo "执行以下命令："
echo "$cmd"
read -p "输入选项(y/n): " num
if [[ $num != "y" ]]; then
    echo "取消执行"
    exit
fi

 while IFS= read -r ip
  do
    ip_array+=("$ip")
  done < servers.txt

  # 执行指令
  echo "正在执行："
  for ip in "${ip_array[@]}"
  do
    echo "------$ip-------"
    #需要绕过主机公钥验证时用这个
     ssh -o StrictHostKeyChecking=no "$ip" "$cmd"
    #ssh -o StrictHostKeyChecking=no  "$ip" 'bash -s' <./add-job.sh
  done
~                                      
